package com.sparrow.mvc.ui;

import javax.servlet.jsp.tagext.TagSupport;
import com.sparrow.core.asm.MethodAccessor;
import com.sparrow.ioc.BeanFactory;
import com.sparrow.utils.StringUtil;

@SuppressWarnings("serial")
public class JWebControl extends TagSupport {
	private String id;
	private String name;
	private String cssClass = "";
	private String cssText = "";
	private String events = "";
	private String title = "";
	private String tagName;
	private String visible = "true";

	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public void setId(String id) {
		this.id = id;
	}

	public String getCtrlName() {
		if (this.name == null) {
			return "";
		} else {
			return this.name;
		}
	}

	public String getName() {
		if (this.name != null && !this.name.trim().equals("")) {
			return String.format(" name=\"%1$s\" ", this.name);
		} else {
			return "";
		}
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCssClass() {
		String className = this.cssClass;
		Object requestCssClass = this.pageContext.getRequest().getAttribute(
				this.getId() + ".cssClass");
		if (requestCssClass != null) {
			className = String.format(" class=\"%1$s\" ", requestCssClass);
		} else {
			if (!StringUtil.isNullOrEmpty(this.cssClass)) {
				className = String.format(" class=\"%1$s\" ", this.cssClass);
			}
		}
		return className;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getCssText() {
		String cssText = this.cssText;
		Object requestCssText = this.pageContext.getRequest().getAttribute(
				this.getId() + ".cssText");
		if (requestCssText != null) {
			cssText = String.format(" style=\"%1$s\" ",
					requestCssText.toString());

		} else {
			if (!StringUtil.isNullOrEmpty(this.cssText)) {
				cssText = String.format(" style=\"%1$s\" ", this.cssText);
			}
		}
		return cssText;
	}

	public void setCssText(String cssText) {
		this.cssText = cssText;
	}

	public String getEvents() {
		String events = "";
		Object requestEvents = this.pageContext.getRequest().getAttribute(
				this.getId() + ".events");
		if (requestEvents != null) {
			events = requestEvents.toString();
		} else {
			if (!StringUtil.isNullOrEmpty(this.events)) {
				events = this.events.trim().replaceAll("\\s", "");
			}
		}
		return events;
	}

	public void setEvents(String events) {
		this.events = events;
	}

	public String getTitle() {
		String title = this.title;
		Object requestTitle = this.pageContext.getRequest().getAttribute(
				this.getId() + ".title");
		if (requestTitle != null) {
			title = String.format(" title=\"%1$s\" ", requestTitle.toString());
		} else {
			if (!StringUtil.isNullOrEmpty(this.title)) {
				title = String.format(" title=\"%1$s\" ", this.title);
			}
		}
		return title;
	}

	public void setTitle(String title) {
		this.title = title;

	}

	public String getTagName() {
		if (this.tagName == null) {
			return "input";
		} else {
			return tagName;
		}
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getVisible() {
		String result = this.visible;
		Object requestVisible = this.pageContext.getRequest().getAttribute(
				this.getId() + ".visible");

		if (requestVisible != null) {
			result = requestVisible.toString();
		}
		return result;
	}

	public void setVisible(String visible) {
		this.visible = visible;
	}

	public String getRequestValue(Object requestValue) {
		if (requestValue == null) {
			requestValue = this.pageContext.getRequest().getAttribute(
					this.getCtrlName());
		}
		// 如果id.vlaue没有设置则获取model.name形式,需要反射
		if (requestValue == null && this.getCtrlName().contains(".")) {
			String[] modelArray = this.getCtrlName().split("\\.");
			String propertyName = modelArray[0].toLowerCase();
			requestValue = this.pageContext.getRequest().getAttribute(
					propertyName);
			if (requestValue != null) {
				try {
					MethodAccessor methodAccessor = BeanFactory.getInstance()
							.getProxyBean(
									requestValue.getClass().getSimpleName());
					String getMethodName = "get"
							+ StringUtil.setFirstByteUpperCase(modelArray[1]);
					requestValue = methodAccessor.get(requestValue,
							getMethodName);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		// 如果model.name形式还没有，则获取请求参数，原样返回
		if (requestValue == null) {
			requestValue = this.pageContext.getRequest().getParameter(
					this.getCtrlName());
		}
		return (String) requestValue;
	}
}
